package com.controller;

import com.github.pagehelper.PageInfo;
import com.javabean.Product;
import com.service.ProductManagerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductManagerService productManagerServiceImpl;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5") int limit,String productName){

        PageInfo<Product> pageInfo = productManagerServiceImpl.findAll(page, limit, productName);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("value",productName);
        return "/product/product-list";
    }
    @RequestMapping("/findById")
    public String findById(HttpServletRequest request,int id){
        Product product = productManagerServiceImpl.findById(id);
        request.setAttribute("product",product);
        return "/product/product-edit";
    }
    @RequestMapping("/edit")
    public String edit(Product product){
        productManagerServiceImpl.editProduct(product);
        //重新跳转到原页面 转发到对应的服务
        return "redirect:findAll";
    }

    @RequestMapping("/add")
    public String add(Product product){
        productManagerServiceImpl.addProduct(product);
        //重新跳转到原页面 转发到对应的服务
        return "redirect:findAll";
    }

    @RequestMapping("/show")
    public String show(HttpServletRequest request,int id){
        Product product = productManagerServiceImpl.findById(id);
        request.setAttribute("product",product);
        return "/product/product-show";
    }


    @RequestMapping("/deleteById")
    public String deleteById(HttpServletRequest request,int id){
        productManagerServiceImpl.removeProduct(id);

        return "redirect:findAll";
    }

    @RequestMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody ArrayList<Integer> ids){
        productManagerServiceImpl.removeListProduct(ids);
        return "/product/findAll";
    }



}
